package com.katze.boot.quartz.controller;

import com.katze.boot.common.exception.BusinessException;
import com.katze.boot.plugins.security.annotations.RestControllerMapping;
import com.katze.boot.plugins.security.annotations.RestRequestMapping;
import com.katze.boot.quartz.domain.JobLog;
import com.katze.boot.quartz.service.JobLogService;
import com.katze.common.Message;
import com.katze.common.Pagination;
import com.katze.common.controller.BaseController;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@RestControllerMapping("/job/log")
public class JobLogController extends BaseController {

    @Autowired
    private JobLogService jobLogService;

    @RestRequestMapping(permits = "jobLog:view", skip = true)
    public Message jobLogList(Pagination request, JobLog log) {
        return toDataTable(this.jobLogService.findJobLogs(request, log));
    }

    @RestRequestMapping(method = RequestMethod.DELETE, permits = "jobLog:delete", description = "删除调度日志")
    public Message deleteJobLog(String[] logIds) {
        this.jobLogService.deleteJobLogs(logIds);
        return Message.ok("任务日志清除成功");
    }

    @RestRequestMapping(value = "/excel", method = RequestMethod.POST, permits = "jobLog:export")
    public void export(Pagination request, JobLog jobLog, HttpServletResponse response) throws BusinessException {
        try {
            List<JobLog> jobLogs = this.jobLogService.findJobLogs(request, jobLog).getRecords();
            super.downXlsx("调度日志信息表", jobLogs, response);
        } catch (Exception e) {
            throw new BusinessException("导出Excel失败", e);
        }
    }
}
